package com.ovopark.organize.server.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ovopark.organize.server.mapper.FranchiseesRegistrationPlanMapper;
import com.ovopark.organize.server.model.entity.FranchiseesRegistration;
import com.ovopark.organize.server.model.entity.FranchiseesRegistrationFollow;
import com.ovopark.organize.server.model.entity.FranchiseesRegistrationPlan;
import com.ovopark.organize.server.model.vo.FranchiseesRegistrationFollowVo;
import com.ovopark.organize.server.service.FranchiseesRegistrationPlanService;
import com.ovopark.organize.server.service.FranchiseesRegistrationService;
import com.ovopark.organize.server.util.CollectionUtil;
import com.ovopark.organize.server.util.CommonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FranchiseesRegistrationPlanServiceImpl extends ServiceImpl<FranchiseesRegistrationPlanMapper, FranchiseesRegistrationPlan> implements FranchiseesRegistrationPlanService {

    Logger logger = LoggerFactory.getLogger(FranchiseesRegistrationPlanServiceImpl.class);

    @Resource
    private FranchiseesRegistrationService franchiseesRegistrationService;

    @Override
    public FranchiseesRegistrationPlan saveOrUpdateBean(FranchiseesRegistrationPlan follow) {
        if(follow.getId() !=null){
            baseMapper.updateById(follow);
        }else {
            baseMapper.insert(follow);
        }
        return follow;
    }

    @Override
    public FranchiseesRegistrationFollowVo saveOrUpdateVo(FranchiseesRegistrationFollowVo plan) {
        FranchiseesRegistrationPlan registrationPlan = new FranchiseesRegistrationPlan();
        BeanUtils.copyProperties(plan,registrationPlan);
        registrationPlan =  saveOrUpdateBean(registrationPlan);
        plan.setId(registrationPlan.getId());
        return plan;
    }

    @Override
    public FranchiseesRegistrationPlan getFollowById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public Boolean deletePlanById(Integer id, Integer groupId) {
        // 删除 跟进记录 如果 当前跟进记录是 最新的一个， 需要把 线索的 跟进人 换成 后面一个；
        FranchiseesRegistrationPlan follow = getFollowById(id);
        if(groupId !=null && follow !=null && follow.getGroupId().intValue() != groupId){
            return false;
        }
        FranchiseesRegistration franchiseesRegistration = franchiseesRegistrationService.findFranchiseesRegistrationById(follow.getFranchiseesRegistrationId());
        baseMapper.deleteById(id);
        if(franchiseesRegistration !=null){
            String operatorType = "删除跟进计划";
            franchiseesRegistrationService.updateFllowUser(follow.getFranchiseesRegistrationId(),operatorType);
        }
        return true;
    }

    @Override
    public List<FranchiseesRegistrationPlan> getRegistrationPlan(Integer registrationId, Integer followUpPlanStatus) {
        return baseMapper.getRegistrationPlan(registrationId,followUpPlanStatus);
    }

    @Override
    public Integer getNumByRegistrationId(Integer registrationId) {
        LambdaQueryWrapper<FranchiseesRegistrationPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FranchiseesRegistrationPlan::getFranchiseesRegistrationId,registrationId);
        queryWrapper.eq(FranchiseesRegistrationPlan::getIsDel,0);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public List<Integer> findAllGroupIds() {
        List<Integer> groupIds = new ArrayList<>();
        LambdaQueryWrapper<FranchiseesRegistrationPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FranchiseesRegistrationPlan::getIsDel,0);
        queryWrapper.eq(FranchiseesRegistrationPlan::getFollowUpPlanStatus,FranchiseesRegistrationFollow.FOLLOW_UP_STATUS_NOT_STARTED);
        queryWrapper.eq(FranchiseesRegistrationPlan::getFollowUpPlan,1);
        queryWrapper.select(FranchiseesRegistrationPlan::getGroupId);
        List<FranchiseesRegistrationPlan> franchiseesRegistrationFollows = baseMapper.selectList(queryWrapper);
        if(CollectionUtil.isNotEmpty(franchiseesRegistrationFollows)){
            groupIds = franchiseesRegistrationFollows.stream().map(FranchiseesRegistrationPlan::getGroupId).distinct().collect(Collectors.toList());
            groupIds = groupIds.stream().distinct().collect(Collectors.toList());
        }
        return groupIds;
    }

    @Override
    public void synChangeRegistrationPlanStatus(Integer groupId) {
        LambdaQueryWrapper<FranchiseesRegistrationPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FranchiseesRegistrationPlan::getGroupId,groupId);
        queryWrapper.eq(FranchiseesRegistrationPlan::getIsDel,0);
        queryWrapper.eq(FranchiseesRegistrationPlan::getFollowUpPlanStatus,FranchiseesRegistrationFollow.FOLLOW_UP_STATUS_NOT_STARTED);
        queryWrapper.eq(FranchiseesRegistrationPlan::getFollowUpPlan,1);
        List<FranchiseesRegistrationPlan> franchiseesRegistrationFollows = baseMapper.selectList(queryWrapper);
        if(CollectionUtil.isNotEmpty(franchiseesRegistrationFollows)){
            for (FranchiseesRegistrationPlan follow:franchiseesRegistrationFollows ) {
                Date nextFollowTime = follow.getNextFollowTime();
                if(nextFollowTime !=null && nextFollowTime.before(new Date())){
                    follow.setFollowUpPlanStatus(FranchiseesRegistrationFollow.FOLLOW_UP_STATUS_OVERDUE);
                    baseMapper.updateById(follow);
                }

            }

        }
    }

    @Override
    public List<FranchiseesRegistrationPlan> getPlanByRegistrationId(Integer registrationId, Boolean filterClose) {
        LambdaQueryWrapper<FranchiseesRegistrationPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FranchiseesRegistrationPlan::getIsDel,0);
        queryWrapper.eq(FranchiseesRegistrationPlan::getFranchiseesRegistrationId,registrationId);
        if(filterClose){
            queryWrapper.ne(FranchiseesRegistrationPlan::getFollowUpPlanStatus,FranchiseesRegistrationFollow.FOLLOW_UP_STATUS_CANCEL);
        }
        queryWrapper.orderByDesc(FranchiseesRegistrationPlan::getFollowTime);
        return  baseMapper.selectList(queryWrapper);
    }

    @Override
    public void updateFollowUpPlanStatusByRegistrationId(Integer groupId, List<Integer> registrationIds, Integer userId, Integer followUpStatusCancel, String followUpPlanReason) {
        if (CollectionUtil.isNotEmpty(registrationIds)) {
            baseMapper.updateFollowUpPlanStatusByRegistrationId(groupId, CommonUtil.IntegerToStringSql(registrationIds), userId, followUpStatusCancel, followUpPlanReason);
        }
    }


}
